package com.edu.juc.assistClass;

import java.util.concurrent.CountDownLatch;

// 减少计数器，减到0时，再执行任务
// 演示 CountDownLatch
public class CountDownLatchTest {
    // 6个同学陆续离开教室后，班长才能锁门
    public static void main(String[] args) {
        CountDownLatch countDownLatch = new CountDownLatch(6);

        new Thread(() -> {
            try {
                countDownLatch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "锁门。");
        }, "班长").start();

        for (int i = 1; i <= 6; i++) {
            new Thread(() -> {
                System.out.println(Thread.currentThread().getName() + "离开。");
                countDownLatch.countDown(); // -1
            }, "Student " + i).start();
        }
    }
}
